home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 007 (1987-02-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 007 (1987-02-15)(Ossowski, Stefan)(DE)(PD).adf / C Compiler / expr.c < prev    next >
C/C++ Source or Header  |  1987-03-04  |  35KB  |  949 lines

  1. #include        <stdio.h>
  2. #include        "c.h"
  3. #include        "expr.h"
  4. #include        "gen.h"
  5. #include        "cglbdec.h"
  6.  
  7. /*
  8.  *    68000 C compiler
  9.  *
  10.  *    Copyright 1984, 1985, 1986 Matthew Brandt.
  11.  *    all commercial rights reserved.
  12.  *
  13.  *    This compiler is intended as an instructive tool for personal use. Any
  14.  *    use for profit without the written consent of the author is prohibited.
  15.  *
  16.  *    This compiler may be distributed freely for non-commercial use as long
  17.  *    as this notice stays intact. Please forward any enhancements or questions
  18.  *    to:
  19.  *
  20.  *        Matthew Brandt
  21.  *        Box 920337
  22.  *        Norcross, Ga 30092
  23.  */
  24.  
  25. TYP             stdint = { bt_long, 0, 4, {0, 0}, 0, 0 };
  26. TYP             stdchar = {bt_char, 0, 1, {0, 0}, 0, 0 };
  27. TYP             stdstring = {bt_pointer, 1, 4, {0, 0}, &stdchar, 0};
  28. TYP             stdfunc = {bt_func, 1, 0, {0, 0}, &stdint, 0};
  29. extern TYP      *head;          /* shared with decl */
  30.  
  31. /*
  32.  *      expression evaluation
  33.  *
  34.  *      this set of routines builds a parse tree for an expression.
  35.  *      no code is generated for the expressions during the build,
  36.  *      this is the job of the codegen module. for most purposes
  37.  *      expression() is the routine to call. it will allow all of
  38.  *      the C operators. for the case where the comma operator is
  39.  *      not valid (function parameters for instance) call exprnc().
  40.  *
  41.  *      each of the routines returns a pointer to a describing type
  42.  *      structure. each routine also takes one parameter which is a
  43.  *      pointer to an expression node by reference (address of pointer).
  44.  *      the completed expression is returned in this pointer. all
  45.  *      routines return either a pointer to a valid type or NULL if
  46.  *      the hierarchy of the next operator is too low or the next
  47.  *      symbol is not part of an expression.
  48.  */
  49.  
  50. TYP     *expression();  /* forward declaration */
  51. TYP     *exprnc();      /* forward declaration */
  52. TYP     *unary();       /* forward declaration */
  53.  
  54. struct enode    *makenode(nt, v1, v2)
  55. /*
  56.  *      build an expression node with a node type of nt and values
  57.  *      v1 and v2.
  58.  */
  59. enum e_node nt;
  60.  
  61. char  *v1, *v2;
  62. {       struct enode    *ep;
  63.         ep = xalloc(sizeof(struct enode));
  64.         ep->nodetype = nt;
  65.         ep->constflag = 0;
  66.         ep->v.p[0] = v1;
  67.         ep->v.p[1] = v2;
  68.         return ep;
  69. }
  70.  
  71. deref(node,tp)
  72. /*
  73.  *      build the proper dereference operation for a node using the
  74.  *      type pointer tp.
  75.  */
  76. struct enode    **node;
  77. TYP             *tp;
  78. {       switch( tp->type ) {
  79.                 case bt_char:
  80.                         *node = makenode(en_b_ref,*node,0);
  81.                         tp = &stdint;
  82.                         break;
  83.                 case bt_short:
  84.                 case bt_enum:
  85.                         *node = makenode(en_w_ref,*node,0);
  86.                         tp = &stdint;
  87.                         break;
  88.                 case bt_long:
  89.                 case bt_pointer:
  90.                 case bt_unsigned:
  91.                         *node = makenode(en_l_ref,*node,0);
  92.                         break;
  93.                 default:
  94.                         error(ERR_DEREF);
  95.                         break;
  96.                 }
  97.         return tp;
  98. }
  99.  
  100. TYP     *nameref(node)
  101. /*
  102.  *      nameref will build an expression tree that references an
  103.  *      identifier. if the identifier is not in the global or
  104.  *      local symbol table then a look-ahead to the next character
  105.  *      is done and if it indicates a function call the identifier
  106.  *      is coerced to an external function name. non-value references
  107.  *      generate an additional level of indirection.
  108.  */
  109. struct enode    **node;
  110. {       SYM             *sp;
  111.         TYP             *tp;
  112.         sp = gsearch(lastid);
  113.         if( sp == 0 ) {
  114.                 while( isspace(lastch) )
  115.                         getch();
  116.                 if( lastch == '(') {
  117.                         ++global_flag;
  118.                         sp = xalloc(sizeof(SYM));
  119.                         sp->tp = &stdfunc;
  120.                         sp->name = litlate(lastid);
  121.                         sp->storage_class = sc_external;
  122.                         insert(sp,&gsyms);
  123.                         --global_flag;
  124.                         tp = &stdfunc;
  125.                         *node = makenode(en_nacon,sp->name,0);
  126.                         (*node)->constflag = 1;
  127.                         }
  128.                 else    {
  129.                         tp = 0;
  130.                         error(ERR_UNDEFINED);
  131.                         }
  132.                 }
  133.         else    {
  134.                 if( (tp = sp->tp) == 0 ) {
  135.                         error(ERR_UNDEFINED);
  136.                         return 0;       /* guard against untyped entries */
  137.                         }
  138.                 switch( sp->storage_class ) {
  139.                         case sc_static:
  140.                                 *node = makenode(en_labcon,sp->value.i,0);
  141.                                 (*node)->constflag = 1;
  142.                                 break;
  143.                         case sc_global:
  144.                         case sc_external:
  145.                                 *node = makenode(en_nacon,sp->name,0);
  146.                                 (*node)->constflag = 1;
  147.                                 break;
  148.                         case sc_const:
  149.                                 *node = makenode(en_icon,sp->value.i,0);
  150.                                 (*node)->constflag = 1;
  151.                                 break;
  152.                         default:        /* auto and any errors */
  153.                                 if( sp->storage_class != sc_auto)
  154.                                         error(ERR_ILLCLASS);
  155.                                 *node = makenode(en_autocon,sp->value.i,0);
  156.                                 break;
  157.                         }
  158.                 if( tp->val_flag == 0)
  159.                         tp = deref(node,tp);
  160.                 }
  161.         getsym();
  162.         return tp;
  163. }
  164.  
  165. struct enode    *parmlist()
  166. /*
  167.  *      parmlist will build a list of parameter expressions in
  168.  *      a function call and return a pointer to the last expression
  169.  *      parsed. since parameters are generally pushed from right
  170.  *      to left we get just what we asked for...
  171.  */
  172. {       struct enode    *ep1, *ep2;
  173.         ep1 = 0;
  174.         while( lastst != closepa) {
  175.                 exprnc( &ep2);          /* evaluate a parameter */
  176.                 ep1 = makenode(en_void,ep2,ep1);
  177.                 if( lastst != comma)
  178.                         break;
  179.                 getsym();
  180.                 }
  181.         return ep1;
  182. }
  183.  
  184. int     castbegin(st)
  185. /*
  186.  *      return 1 if st in set of [ kw_char, kw_short, kw_long, kw_int,
  187.  *      kw_float, kw_double, kw_struct, kw_union ]
  188.  */
  189. enum e_sym st;
  190. {       return  st == kw_char || st == kw_short || st == kw_int ||
  191.                 st == kw_long || st == kw_float || st == kw_double ||
  192.                 st == kw_struct || st == kw_union || st== kw_unsigned;
  193. }
  194.  
  195. TYP     *primary(node)
  196. /*
  197.  *      primary will parse a primary expression and set the node pointer
  198.  *      returning the type of the expression parsed. primary expressions
  199.  *      are any of:
  200.  *                      id
  201.  *                      constant
  202.  *                      string
  203.  *                      ( expression )
  204.  *                      primary[ expression ]
  205.  *                      primary.id
  206.  *                      primary->id
  207.  *                      primary( parameter list )
  208.  */
  209. struct enode    **node;
  210. {       struct enode    *pnode, *qnode, *rnode;
  211.         SYM             *sp;
  212.         TYP             *tptr;
  213.         switch( lastst ) {
  214.  
  215.                 case id:
  216.                         tptr = nameref(&pnode);
  217.                         break;
  218.                 case iconst:
  219.                         tptr = &stdint;
  220.                         pnode = makenode(en_icon,ival,0);
  221.                         pnode->constflag = 1;
  222.                         getsym();
  223.                         break;
  224.                 case sconst:
  225.                         tptr = &stdstring;
  226.                         pnode = makenode(en_labcon,stringlit(laststr),0);
  227.                         pnode->constflag = 1;
  228.                         getsym();
  229.                         break;
  230.                 case openpa:
  231.                         getsym();
  232.                         if( !castbegin(lastst) ) {
  233.                                 tptr = expression(&pnode);
  234.                                 needpunc(closepa);
  235.                                 }
  236.                         else    {       /* cast operator */
  237.                                 decl(); /* do cast declaration */
  238.                                 decl1();
  239.                                 tptr = head;
  240.                                 needpunc(closepa);
  241.                                 if( unary(&pnode) == 0 ) {
  242.                                         error(ERR_IDEXPECT);
  243.                                         tptr = 0;
  244.                                         }
  245.                                 }
  246.                         break;
  247.                 default:
  248.                         return 0;
  249.                 }
  250.         for(;;) {
  251.                 switch( lastst ) {
  252.                         case openbr:    /* build a subscript reference */
  253.                                 if( tptr->type != bt_pointer )
  254.                                         error(ERR_NOPOINTER);
  255.                                 else
  256.                                         tptr = tptr->btp;
  257.                                 getsym();
  258.                                 qnode = makenode(en_icon,tptr->size,0);
  259.                                 qnode->constflag = 1;
  260.                                 expression(&rnode);
  261. /*
  262.  *      we could check the type of the expression here...
  263.  */
  264.                                 qnode = makenode(en_mul,qnode,rnode);
  265.                                 qnode->constflag = rnode->constflag &&
  266.                                         qnode->v.p[0]->constflag;
  267.                                 pnode = makenode(en_add,qnode,pnode);
  268.                                 pnode->constflag = qnode->constflag &&
  269.                                         pnode->v.p[1]->constflag;
  270.                                 if( tptr->val_flag == 0 )
  271.                                         tptr = deref(&pnode,tptr);
  272.                                 needpunc(closebr);
  273.                                 break;
  274.                         case pointsto:
  275.                                 if( tptr->type != bt_pointer )
  276.                                         error(ERR_NOPOINTER);
  277.                                 else
  278.                                         tptr = tptr->btp;
  279.                                 if( tptr->val_flag == 0 )
  280.                                         pnode = makenode(en_l_ref,pnode,0);
  281. /*
  282.  *      fall through to dot operation
  283.  */
  284.                         case dot:
  285.                                 getsym();       /* past -> or . */
  286.                                 if( lastst != id )
  287.                                         error(ERR_IDEXPECT);
  288.                                 else    {
  289.                                         sp = search(lastid,tptr->lst.head);
  290.                                         if( sp == 0 )
  291.                                                 error(ERR_NOMEMBER);
  292.                                         else    {
  293.                                                 tptr = sp->tp;
  294.                                                 qnode = makenode(en_icon,sp->value.i,0);
  295.                                                 qnode->constflag = 1;
  296.                                                 pnode = makenode(en_add,pnode,qnode);
  297.                                                 pnode->constflag = pnode->v.p[0]->constflag;
  298.                                                 if( tptr->val_flag == 0 )
  299.                                                     tptr = deref(&pnode,tptr);
  300.                                                 }
  301.                                         getsym();       /* past id */
  302.                                         }
  303.                                 break;
  304.                         case openpa:    /* function reference */
  305.                                 getsym();
  306.                                 if( tptr->type != bt_func &&
  307.                                         tptr->type != bt_ifunc )
  308.                                         error(ERR_NOFUNC);
  309.                                 else
  310.                                         tptr = tptr->btp;
  311.                                 pnode = makenode(en_fcall,pnode,parmlist());
  312.                                 needpunc(closepa);
  313.                                 break;
  314.                         default:
  315.                                 goto fini;
  316.                         }
  317.                 }
  318. fini:   *node = pnode;
  319.         return tptr;
  320. }
  321.  
  322. int     lvalue(node)
  323. /*
  324.  *      this function returns true if the node passed is an lvalue.
  325.  *      this can be qualified by the fact that an lvalue must have
  326.  *      one of the dereference operators as it's top node.
  327.  */
  328. struct enode    *node;
  329. {       switch( node->nodetype ) {
  330.                 case en_b_ref:
  331.                 case en_w_ref:
  332.                 case en_l_ref:
  333.                 case en_ub_ref:
  334.                 case en_uw_ref:
  335.                 case en_ul_ref:
  336.                         return 1;
  337.                 case en_cbl:
  338.                 case en_cwl:
  339.                 case en_cbw:
  340.                         return lvalue(node->v.p[0]);
  341.                 }
  342.         return 0;
  343. }
  344.  
  345. TYP     *unary(node)
  346. /*
  347.  *      unary evaluates unary expressions and returns the type of the
  348.  *      expression evaluated. unary expressions are any of:
  349.  *
  350.  *                      primary
  351.  *                      primary++
  352.  *                      primary--
  353.  *                      !unary
  354.  *                      ~unary
  355.  *                      ++unary
  356.  *                      --unary
  357.  *                      -unary
  358.  *                      *unary
  359.  *                      &unary
  360.  *                      (typecast)unary
  361.  *                      sizeof(typecast)
  362.  *
  363.  */
  364. struct enode    **node;
  365. {       TYP             *tp, *tp1;
  366.         struct enode    *ep1, *ep2;
  367.         int             flag, i;
  368.         flag = 0;
  369.         switch( lastst ) {
  370.                 case autodec:
  371.                         flag = 1;
  372.                 /* fall through to common increment */
  373.                 case autoinc:
  374.                         getsym();
  375.                         tp = unary(&ep1);
  376.                         if( tp == 0 ) {
  377.                                 error(ERR_IDEXPECT);
  378.                                 return 0;
  379.                                 }
  380.                         if( lvalue(ep1)) {
  381.                                 if( tp->type == bt_pointer )
  382.                                         ep2 = makenode(en_icon,tp->btp->size,0);
  383.                                 else
  384.                                         ep2 = makenode(en_icon,1,0);
  385.                                 ep2->constflag = 1;
  386.                                 ep1 = makenode(flag ? en_assub : en_asadd,ep1,ep2);
  387.                                 }
  388.                         else
  389.                                 error(ERR_LVALUE);
  390.                         break;
  391.                 case minus:
  392.                         getsym();
  393.                         tp = unary(&ep1);
  394.                         if( tp == 0 ) {
  395.                                 error(ERR_IDEXPECT);
  396.                                 return 0;
  397.                                 }
  398.                         ep1 = makenode(en_uminus,ep1,0);
  399.                         ep1->constflag = ep1->v.p[0]->constflag;
  400.                         break;
  401.                 case not:
  402.                         getsym();
  403.                         tp = unary(&ep1);
  404.                         if( tp == 0 ) {
  405.                                 error(ERR_IDEXPECT);
  406.                                 return 0;
  407.                                 }
  408.                         ep1 = makenode(en_not,ep1,0);
  409.                         ep1->constflag = ep1->v.p[0]->constflag;
  410.                         break;
  411.                 case compl:
  412.                         getsym();
  413.                         tp = unary(&ep1);
  414.                         if( tp == 0 ) {
  415.                                 error(ERR_IDEXPECT);
  416.                                 return 0;
  417.                                 }
  418.                         ep1 = makenode(en_compl,ep1,0);
  419.                         ep1->constflag = ep1->v.p[0]->constflag;
  420.                         break;
  421.                 case star:
  422.                         getsym();
  423.                         tp = unary(&ep1);
  424.                         if( tp == 0 ) {
  425.                                 error(ERR_IDEXPECT);
  426.                                 return 0;
  427.                                 }
  428.                         if( tp->btp == 0 )
  429.                                 error(ERR_DEREF);
  430.                         else
  431.                                 tp = tp->btp;
  432.                         if( tp->val_flag == 0 )
  433.                                 tp = deref(&ep1,tp);
  434.                         break;
  435.                 case and:
  436.                         getsym();
  437.                         tp = unary(&ep1);
  438.                         if( tp == 0 ) {
  439.                                 error(ERR_IDEXPECT);
  440.                                 return 0;
  441.                                 }
  442.                         if( lvalue(ep1))
  443.                                 ep1 = ep1->v.p[0];
  444.                         tp1 = xalloc(sizeof(TYP));
  445.                         tp1->size = 4;
  446.                         tp1->type = bt_pointer;
  447.                         tp1->btp = tp;
  448.                         tp1->val_flag = 0;
  449.                         tp1->lst.head = 0;
  450.                         tp1->sname = 0;
  451.                         tp = tp1;
  452.                         break;
  453.                 case kw_sizeof:
  454.                         getsym();
  455.                         needpunc(openpa);
  456.                         decl();
  457.                         decl1();
  458.                         if( head != 0 )
  459.                                 ep1 = makenode(en_icon,head->size,0);
  460.                         else    {
  461.                                 error(ERR_IDEXPECT);
  462.                                 ep1 = makenode(en_icon,1,0);
  463.                                 }
  464.                         ep1->constflag = 1;
  465.                         tp = &stdint;
  466.                         needpunc(closepa);
  467.                         break;
  468.                 default:
  469.                         tp = primary(&ep1);
  470.                         if( tp != 0 ) {
  471.                                 if( tp->type == bt_pointer )
  472.                                         i = tp->btp->size;
  473.                                 else
  474.                                         i = 1;
  475.                                 if( lastst == autoinc) {
  476.                                         if( lvalue(ep1) )
  477.                                                 ep1 = makenode(en_ainc,ep1,i);
  478.                                         else
  479.                                                 error(ERR_LVALUE);
  480.                                         getsym();
  481.                                         }
  482.                                 else if( lastst == autodec ) {
  483.                                         if( lvalue(ep1) )
  484.                                                 ep1 = makenode(en_adec,ep1,i);
  485.                                         else
  486.                                                 error(ERR_LVALUE);
  487.                                         getsym();
  488.                                         }
  489.                                 }
  490.                         break;
  491.                 }
  492.         *node = ep1;
  493.         return tp;
  494. }
  495.  
  496. TYP     *forcefit(node1,tp1,node2,tp2)
  497. /*
  498.  *      forcefit will coerce the nodes passed into compatable
  499.  *      types and return the type of the resulting expression.
  500.  */
  501. struct enode    **node1, **node2;
  502. TYP             *tp1, *tp2;
  503. {       switch( tp1->type ) {
  504.                 case bt_char:
  505.                 case bt_short:
  506.                 case bt_long:
  507.                         if( tp2->type == bt_long ||
  508.                                 tp2->type == bt_short ||
  509.                                 tp2->type == bt_char)
  510.                                 return &stdint;
  511.                         else if( tp2->type == bt_pointer ||
  512.                                 tp2->type == bt_unsigned )
  513.                                 return tp2;
  514.                         break;
  515.                 case bt_pointer:
  516.                         if( isscalar(tp2) || tp2->type == bt_pointer)
  517.                                 return tp1;
  518.                         break;
  519.                 case bt_unsigned:
  520.                         if( tp2->type == bt_pointer )
  521.                                 return tp2;
  522.                         else if( isscalar(tp2) )
  523.                                 return tp1;
  524.                         break;
  525.                 }
  526.         error( ERR_MISMATCH );
  527.         return tp1;
  528. }
  529.  
  530. int     isscalar(tp)
  531. /*
  532.  *      this function returns true when the type of the argument is
  533.  *      one of char, short, unsigned, or long.
  534.  */
  535. TYP             *tp;
  536. {       return  tp->type == bt_char ||
  537.                 tp->type == bt_short ||
  538.                 tp->type == bt_long ||
  539.                 tp->type == bt_unsigned;
  540. }
  541.  
  542. TYP     *multops(node)
  543. /*
  544.  *      multops parses the multiply priority operators. the syntax of
  545.  *      this group is:
  546.  *
  547.  *              unary
  548.  *              multop * unary
  549.  *              multop / unary
  550.  *              multop % unary
  551.  */
  552. struct enode    **node;
  553. {       struct enode    *ep1, *ep2;
  554.         TYP             *tp1, *tp2;
  555.         enum e_sym oper;
  556.         tp1 = unary(&ep1);
  557.         if( tp1 == 0 )
  558.                 return 0;
  559.         while( lastst == star || lastst == divide || lastst == modop ) {
  560.                 oper = lastst;
  561.                 getsym();       /* move on to next unary op */
  562.                 tp2 = unary(&ep2);
  563.                 if( tp2 == 0 ) {
  564.                         error(ERR_IDEXPECT);
  565.                         *node = ep1;
  566.                         return tp1;
  567.                         }
  568.                 tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  569.                 switch( oper ) {
  570.                         case star:
  571.                                 if( tp1->type == bt_unsigned )
  572.                                         ep1 = makenode(en_umul,ep1,ep2);
  573.                                 else
  574.                                         ep1 = makenode(en_mul,ep1,ep2);
  575.                                 break;
  576.                         case divide:
  577.                                 if( tp1->type == bt_unsigned )
  578.                                         ep1 = makenode(en_udiv,ep1,ep2);
  579.                                 else
  580.                                         ep1 = makenode(en_div,ep1,ep2);
  581.                                 break;
  582.                         case modop:
  583.                                 if( tp1->type == bt_unsigned )
  584.                                         ep1 = makenode(en_umod,ep1,ep2);
  585.                                 else
  586.                                         ep1 = makenode(en_mod,ep1,ep2);
  587.                                 break;
  588.                         }
  589.                 ep1->constflag = ep1->v.p[0]->constflag &&
  590.                         ep1->v.p[1]->constflag;
  591.                 }
  592.         *node = ep1;
  593.         return tp1;
  594. }
  595.  
  596. TYP     *addops(node)
  597. /*
  598.  *      addops handles the addition and subtraction operators.
  599.  */
  600. struct enode    **node;
  601. {       struct enode    *ep1, *ep2, *ep3;
  602.         TYP             *tp1, *tp2;
  603.         int             oper, i;
  604.         tp1 = multops(&ep1);
  605.         if( tp1 == 0 )
  606.                 return 0;
  607.         while( lastst == plus || lastst == minus ) {
  608.                 oper = (lastst == plus);
  609.                 getsym();
  610.                 tp2 = multops(&ep2);
  611.                 if( tp2 == 0 ) {
  612.                         error(ERR_IDEXPECT);
  613.                         *node = ep1;
  614.                         return tp1;
  615.                         }
  616.                 if( tp1->type == bt_pointer ) {
  617.                         tp2 = forcefit(0,&stdint,&ep2,tp2);
  618.                         ep3 = makenode(en_icon,tp1->btp->size,0);
  619.                         ep3->constflag = 1;
  620.                         ep2 = makenode(en_mul,ep3,ep2);
  621.                         ep2->constflag = ep2->v.p[1]->constflag;
  622.                         }
  623.                 else if( tp2->type == bt_pointer ) {
  624.                         tp1 = forcefit(0,&stdint,&ep1,tp1);
  625.                         ep3 = makenode(en_icon,tp2->btp->size,0);
  626.                         ep3->constflag = 1;
  627.                         ep1 = makenode(en_mul,ep3,ep1);
  628.                         ep1->constflag = ep1->v.p[1]->constflag;
  629.                         }
  630.                 tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  631.                 ep1 = makenode( oper ? en_add : en_sub,ep1,ep2);
  632.                 ep1->constflag = ep1->v.p[0]->constflag &&
  633.                         ep1->v.p[1]->constflag;
  634.                 }
  635.         *node = ep1;
  636.         return tp1;
  637. }
  638.  
  639. TYP     *shiftop(node)
  640. /*
  641.  *      shiftop handles the shift operators << and >>.
  642.  */
  643. struct enode    **node;
  644. {       struct enode    *ep1, *ep2;
  645.         TYP             *tp1, *tp2;
  646.         int             oper;
  647.         tp1 = addops(&ep1);
  648.         if( tp1 == 0)
  649.                 return 0;
  650.         while( lastst == lshift || lastst == rshift) {
  651.                 oper = (lastst == lshift);
  652.                 getsym();
  653.                 tp2 = addops(&ep2);
  654.                 if( tp2 == 0 )
  655.                         error(ERR_IDEXPECT);
  656.                 else    {
  657.                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  658.                         ep1 = makenode(oper ? en_lsh : en_rsh,ep1,ep2);
  659.                         ep1->constflag = ep1->v.p[0]->constflag &&
  660.                                 ep1->v.p[1]->constflag;
  661.                         }
  662.                 }
  663.         *node = ep1;
  664.         return tp1;
  665. }
  666.  
  667. TYP     *relation(node)
  668. /*
  669.  *      relation handles the relational operators < <= > and >=.
  670.  */
  671. struct enode    **node;
  672. {       struct enode    *ep1, *ep2;
  673.         TYP             *tp1, *tp2;
  674.         int             nt;
  675.         tp1 = shiftop(&ep1);
  676.         if( tp1 == 0 )
  677.                 return 0;
  678.         for(;;) {
  679.                 switch( lastst ) {
  680.  
  681.                         case lt:
  682.                                 if( tp1->type == bt_unsigned )
  683.                                         nt = en_ult;
  684.                                 else
  685.                                         nt = en_lt;
  686.                                 break;
  687.                         case gt:
  688.                                 if( tp1->type == bt_unsigned )
  689.                                         nt = en_ugt;
  690.                                 else
  691.                                         nt = en_gt;
  692.                                 break;
  693.                         case leq:
  694.                                 if( tp1->type == bt_unsigned )
  695.                                         nt = en_ule;
  696.                                 else
  697.                                         nt = en_le;
  698.                                 break;
  699.                         case geq:
  700.                                 if( tp1->type == bt_unsigned )
  701.                                         nt = en_uge;
  702.                                 else
  703.                                         nt = en_ge;
  704.                                 break;
  705.                         default:
  706.                                 goto fini;
  707.                         }
  708.                 getsym();
  709.                 tp2 = shiftop(&ep2);
  710.                 if( tp2 == 0 )
  711.                         error(ERR_IDEXPECT);
  712.                 else    {
  713.                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  714.                         ep1 = makenode(nt,ep1,ep2);
  715.                         ep1->constflag = ep1->v.p[0]->constflag &&
  716.                                 ep1->v.p[1]->constflag;
  717.                         }
  718.                 }
  719. fini:   *node = ep1;
  720.         return tp1;
  721. }
  722.  
  723. TYP     *equalops(node)
  724. /*
  725.  *      equalops handles the equality and inequality operators.
  726.  */
  727. struct enode    **node;
  728. {       struct enode    *ep1, *ep2;
  729.         TYP             *tp1, *tp2;
  730.         int             oper;
  731.         tp1 = relation(&ep1);
  732.         if( tp1 == 0 )
  733.                 return 0;
  734.         while( lastst == eq || lastst == neq ) {
  735.                 oper = (lastst == eq);
  736.                 getsym();
  737.                 tp2 = relation(&ep2);
  738.                 if( tp2 == 0 )
  739.                         error(ERR_IDEXPECT);
  740.                 else    {
  741.                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  742.                         ep1 = makenode( oper ? en_eq : en_ne,ep1,ep2);
  743.                         ep1->constflag = ep1->v.p[0]->constflag &&
  744.                                 ep1->v.p[1]->constflag;
  745.                         }
  746.                 }
  747.         *node = ep1;
  748.         return tp1;
  749. }
  750.  
  751. TYP     *binop(node,xfunc,nt,sy)
  752. /*
  753.  *      binop is a common routine to handle all of the legwork and
  754.  *      error checking for bitand, bitor, bitxor, andop, and orop.
  755.  */
  756. struct enode    **node;
  757. TYP             *(*xfunc)();
  758. int             nt;
  759. enum e_sym sy;
  760. {       struct enode    *ep1, *ep2;
  761.         TYP             *tp1, *tp2;
  762.         tp1 = (*xfunc)(&ep1);
  763.         if( tp1 == 0 )
  764.                 return 0;
  765.         while( lastst == sy ) {
  766.                 getsym();
  767.                 tp2 = (*xfunc)(&ep2);
  768.                 if( tp2 == 0 )
  769.                         error(ERR_IDEXPECT);
  770.                 else    {
  771.                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  772.                         ep1 = makenode(nt,ep1,ep2);
  773.                         ep1->constflag = ep1->v.p[0]->constflag &&
  774.                                 ep1->v.p[1]->constflag;
  775.                         }
  776.                 }
  777.         *node = ep1;
  778.         return tp1;
  779. }
  780.  
  781. TYP     *bitand(node)
  782. /*
  783.  *      the bitwise and operator...
  784.  */
  785. struct enode    **node;
  786. {       return binop(node,equalops,en_and,and);
  787. }
  788.  
  789. TYP     *bitxor(node)
  790. struct enode    **node;
  791. {       return binop(node,bitand,en_xor,uparrow);
  792. }
  793.  
  794. TYP     *bitor(node)
  795. struct enode    **node;
  796. {       return binop(node,bitxor,en_or,or);
  797. }
  798.  
  799. TYP     *andop(node)
  800. struct enode    **node;
  801. {       return binop(node,bitor,en_land,land);
  802. }
  803.  
  804. TYP     *orop(node)
  805. struct enode    **node;
  806. {       return binop(node,andop,en_lor,lor);
  807. }
  808.  
  809. TYP     *conditional(node)
  810. /*
  811.  *      this routine processes the hook operator.
  812.  */
  813. struct enode    **node;
  814. {       TYP             *tp1, *tp2, *tp3;
  815.         struct enode    *ep1, *ep2, *ep3;
  816.         tp1 = orop(&ep1);       /* get condition */
  817.         if( tp1 == 0 )
  818.                 return 0;
  819.         if( lastst == hook ) {
  820.                 getsym();
  821.                 if( (tp2 = conditional(&ep2)) == 0) {
  822.                         error(ERR_IDEXPECT);
  823.                         goto cexit;
  824.                         }
  825.                 needpunc(colon);
  826.                 if( (tp3 = conditional(&ep3)) == 0) {
  827.                         error(ERR_IDEXPECT);
  828.                         goto cexit;
  829.                         }
  830.                 tp1 = forcefit(&ep2,tp2,&ep3,tp3);
  831.                 ep2 = makenode(en_void,ep2,ep3);
  832.                 ep1 = makenode(en_cond,ep1,ep2);
  833.                 }
  834. cexit:  *node = ep1;
  835.         return tp1;
  836. }
  837.  
  838. TYP     *asnop(node)
  839. /*
  840.  *      asnop handles the assignment operators. currently only the
  841.  *      simple assignment is implemented.
  842.  */
  843. struct enode    **node;
  844. {       struct enode    *ep1, *ep2, *ep3;
  845.         TYP             *tp1, *tp2;
  846.         int             op;
  847.         tp1 = conditional(&ep1);
  848.         if( tp1 == 0 )
  849.                 return 0;
  850.         for(;;) {
  851.                 switch( lastst ) {
  852.                         case assign:
  853.                                 op = en_assign;
  854. ascomm:                         getsym();
  855.                                 tp2 = asnop(&ep2);
  856. ascomm2:                        if( tp2 == 0 || !lvalue(ep1) )
  857.                                         error(ERR_LVALUE);
  858.                                 else    {
  859.                                         tp1 = forcefit(&ep1,tp1,&ep2,tp2);
  860.                                         ep1 = makenode(op,ep1,ep2);
  861.                                         }
  862.                                 break;
  863.                         case asplus:
  864.                                 op = en_asadd;
  865. ascomm3:                        tp2 = asnop(&ep2);
  866.                                 if( tp1->type == bt_pointer ) {
  867.                                         ep3 = makenode(en_icon,tp1->btp->size,0);
  868.                                         ep2 = makenode(en_mul,ep2,ep3);
  869.                                         }
  870.                                 goto ascomm;
  871.                         case asminus:
  872.                                 op = en_assub;
  873.                                 goto ascomm3;
  874.                         case astimes:
  875.                                 op = en_asmul;
  876.                                 goto ascomm;
  877.                         case asdivide:
  878.                                 op = en_asdiv;
  879.                                 goto ascomm;
  880.                         case asmodop:
  881.                                 op = en_asmod;
  882.                                 goto ascomm;
  883.                         case aslshift:
  884.                                 op = en_aslsh;
  885.                                 goto ascomm;
  886.                         case asrshift:
  887.                                 op = en_asrsh;
  888.                                 goto ascomm;
  889.                         case asand:
  890.                                 op = en_asand;
  891.                                 goto ascomm;
  892.                         case asor:
  893.                                 op = en_asor;
  894.                                 goto ascomm;
  895.                         default:
  896.                                 goto asexit;
  897.                         }
  898.                 }
  899. asexit: *node = ep1;
  900.         return tp1;
  901. }
  902.  
  903. TYP     *exprnc(node)
  904. /*
  905.  *      evaluate an expression where the comma operator is not legal.
  906.  */
  907. struct enode    **node;
  908. {       TYP     *tp;
  909.         tp = asnop(node);
  910.         if( tp == 0 )
  911.                 *node = 0;
  912.         return tp;
  913. }
  914.  
  915. TYP     *commaop(node)
  916. /*
  917.  *      evaluate the comma operator. comma operators are kept as
  918.  *      void nodes.
  919.  */
  920. struct enode    **node;
  921. {       TYP             *tp1;
  922.         struct enode    *ep1, *ep2;
  923.         tp1 = asnop(&ep1);
  924.         if( tp1 == 0 )
  925.                 return 0;
  926.         if( lastst == comma ) {
  927.                 tp1 = commaop(&ep2);
  928.                 if( tp1 == 0 ) {
  929.                         error(ERR_IDEXPECT);
  930.                         goto coexit;
  931.                         }
  932.                 ep1 = makenode(en_void,ep1,ep2);
  933.                 }
  934. coexit: *node = ep1;
  935.         return tp1;
  936. }
  937.  
  938. TYP     *expression(node)
  939. /*
  940.  *      evaluate an expression where all operators are legal.
  941.  */
  942. struct enode    **node;
  943. {       TYP     *tp;
  944.         tp = commaop(node);
  945.         if( tp == 0 )
  946.                 *node = 0;
  947.         return tp;
  948. }
  949.